package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.query.TeacherSearch;
import com.atguigu.guli.service.edu.feign.OssFileService;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-07-26
 */
@RestController
 //允许跨域访问
@RequestMapping("/admin/edu/teacher")
@Api(tags = "讲师管理模块")
@Slf4j
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @Autowired
    private OssFileService ossFileService;

    @ApiOperation("测试服务调用")
    @GetMapping("test")
    public R test(){
        ossFileService.test();
        return R.ok();
    }

    @GetMapping("/message1")
    public String message1(){
        return "message1";
    }


    @GetMapping("list")
    @ApiOperation("所有讲师列表")
    public R listAll(){
        //{}表示占位符
//        log.info("{}级别日志","info");
////        log.error("{}级别日志","error");
////        log.debug("{}级别日志","debug");
////        log.warn("{}级别日志","warn");

        List<Teacher> list = teacherService.list();
        return R.ok().data("items",list).message("获取讲师列表成功");
    }

    //7、批量删除
    /*
        前端需要提交多个id值：
            方式1：请求参数方式提交 url?id=1&id=2&id=3
                        url?ids=1,2,3
                        @RequestParam List<String> ids
            方式2：请求体 json数组的方式提交 [ 1,2,3]

     */
    @ApiOperation("批量删除讲师")
    @DeleteMapping("batchDel")
//    public R batchDel(@RequestParam List<String> ids){ //如果获取请求参数使用的类型为集合类型 必须使用@RequestParam注解
    public R batchDel(@RequestBody List<String> ids){
//        System.out.println("ids = " + ids);
        //查询要批量删除的讲师非空的头像地址集合
        //参考 阿里云oss的文档 在service-oss中编写一个批量删除的接口
        teacherService.batchDelete(ids);

        teacherService.removeByIds(ids);
        return R.ok();
    }

    /*
    * 分页查询  两个参数 pageNum pageSize
    *   1. 如果每次请求都需要携带的参数 参数类型简答，可以使用路径参数
    *      list/{pageNum}/{pageSize}
    *      访问时，必须指定路径参数，才可以访问路径映射的方法
    *   2. get或者post方式 可以以请求参数的方式携带
    *      url?p1=v2&p2=v2
    *      一般是get方式使用
    *   3. 以json的形式，在请求体中携带参数
    * */
    @ApiOperation("分页讲师列表")
    @GetMapping("list/{pageNum}/{pageSize}")
    public R listPage(@ApiParam(value = "当前页码",required = true) @PathVariable("pageNum") Integer pageNum,
                      @ApiParam(value = "每页记录数",required = true) @PathVariable("pageSize") Integer pageSize,
                      TeacherSearch teacherSearch){
        //Page<Teacher> page = new Page<>(pageNum,pageSize);
        //teacherService.page(page);
        Page<Teacher> page = teacherService.selectPageByCondition(pageNum , pageSize , teacherSearch);

        //page中record代表page中的分页数据集合，total：总记录数
        return R.ok().data("page",page);

    }

    @ApiOperation("根据id查询讲师")
    @GetMapping("getById/{id}")
    public R getTeacherById(@ApiParam(value = "讲师的id",required = true) @PathVariable(value = "id") String id){
        Teacher teacher = teacherService.getById(id);
        if (teacher != null){
            return R.ok().message("查询成功").data("item",teacher);
        }else {
            return R.error().message("未查询到该讲师");
        }
    }

    @ApiOperation(value = "根据id删除讲师")
    @DeleteMapping("deleteById/{id}")
    public R deleteById(@ApiParam(value = "讲师id",required = true) @PathVariable("id") String id){

        //删除图片  远程访问service-oss 删除文件的接口：文件地址 模块avatar
        //ossFileService.deleteFile(path,"avatar");
        teacherService.deleteAvatarById(id);
        //删除讲师
        boolean b = teacherService.removeById(id);
        if (b){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }
    }

    @ApiOperation(value = "新增讲师")
    @PostMapping("save")
    public R insertTeacher(@ApiParam(value = "讲师对象",required = true) @RequestBody Teacher teacher){
        boolean b = teacherService.save(teacher);
        if (b){
            return R.ok().message("添加成功");
        }else{
            return R.error().message("添加失败");
        }
    }

    @ApiOperation("更新讲师信息")
    @PutMapping("updateById")
    public R update(@ApiParam(value = "讲师信息",required = true) @RequestBody Teacher teacher){
        boolean b = teacherService.updateById(teacher);
        if (b){
            return R.ok().message("修改成功");
        }else{
            return R.error().message("修改失败");
        }
    }
}

